<?php
/**
 * @package      OWNServices
 * @subpackage   site
 *
 * @author       Sanawin Tinsuwan
 * @copyright    Copyright (C) 20014 Sanawin Tinsuwan. All rights reserved.
 * @license      http://www.gnu.org/licenses/gpl.html GNU/GPL, see LICENSE.txt
 */

defined('_JEXEC') or die();

jimport('joomla.application.component.modellist');

/**
 * OWNServices Component OWNServices Model
 *
 */
class OWNServicesModelPaidTransactions extends JModelList
{
    /**
     * Model context string.
     *
     * @var    string
     */
    protected $_context = 'com_ownservices.paidtransactions';

    /**
	 * Constructor
	 *
	 * @param	array $config 	An optional associative array of configuration settings.
	*/
	public function __construct($config = array())
	{
		// Set field filter
		if(empty($config['filter_fields']))
		{
			$config['filter_fields'] = array(
				'a.id'
			);
		}
		parent::__construct($config);
	}
	
    /**
     * Method to auto-populate the model state.
     * Note. Calling getState in this method will result in recursion.
     *
     * @return    void
     */
    protected function populateState($ordering = 'a.id', $direction = 'ASC')
    {
        $app = JFactory::getApplication();

		// List state information
		$format = $app->input->getWord('format');
		if ($format == 'feed')
		{
			$limit = $app->get('feed_limit');
		}
		else
		{
			$limit = $app->getUserStateFromRequest('global.list.limit', 'limit', $app->get('list_limit'), 'uint');
		}
		$this->setState('list.limit', $limit);

		$limitstart = $app->input->get('limitstart', 0, 'uint');
		$this->setState('list.start', $limitstart);

		// Optional filter text
		$this->setState('list.filter', $app->input->getString('filter-search'));

		// Load the parameters. Merge Global and Menu Item params into new object
		$params = $app->getParams();
		$menuParams = new JRegistry;

		if ($menu = $app->getMenu()->getActive())
		{
			$menuParams->loadString($menu->params);
		}

		$mergedParams = clone $menuParams;
		$mergedParams->merge($params);

		$this->setState('params', $mergedParams);

		// filter.order
		$orderCol = $app->getUserStateFromRequest($this->context . '.filter_order', 'filter_order', '', 'string');
		if (!in_array($orderCol, $this->filter_fields))
		{
			$orderCol = 'a.id';
		}
		$this->setState('list.ordering', $orderCol);

		$listOrder = $app->getUserStateFromRequest($this->context. '.filter_order_Dir', 'filter_order_Dir', '', 'cmd');
		if (!in_array(strtoupper($listOrder), array('ASC', 'DESC', '')))
		{
			$listOrder = 'ASC';
		}
		$this->setState('list.direction', $listOrder);
    }

	/**
	 * Method to get a store id based on model configuration state.
	 *
     * This is necessary because the model is used by the component and
     * different modules that might need different sets of data or different
     * ordering requirements.
     *
     * @param     string    $id    A prefix for the store id.
     *
     * @return    string           A store id.
     */
    protected function getStoreId($id = '')
    {

		return parent::getStoreId($id);
	}
	
	/**
	 * Method to get a list of items.
	 * Override to inject convert the attribs field into a JParameter object. 
	 *
	 * @return mixed $items An array of objects on success, false on failure
	*/
	 public function getItems()
    {
		$items	= parent::getItems();
		return $items;
	}
	
	/**
	 * Get the master query for retrieving a list of items subject to the model state.
	 *
	 * @return database query
	 *
	*/
	protected function getListQuery()
	{
		$query 	= $this->_db->getQuery(true);
		$user 	= JFactory::getUser();
		
		// Select the required fields from the table.
		$query->select(
			$this->getState('list.select',
				'a.id'
			)
		);
		
		$query->from('#__own_transactions AS a');
		
		// Group by ID
		$query->group('a.id');

		// Add the list ordering clause.
        $query->order($this->getState('list.ordering', 'a.id') . ' ' . $this->getState('list.direction', 'ASC'));
		
		return $query;
	}
}